package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-01-16 23:43
 **/
public class LeetCode2141 {

    public static void main(String[] args) {
        LeetCode2141 code = new LeetCode2141();
        code.maxRunTime(2, new int[]{3, 3, 3});
    }

    public long maxRunTime(int n, int[] batteries) {
        long l = 1L;
        long r = 1000000000000000L;
        while (l < r) {
            long m = l + r >> 1;
            if (check(m, batteries, n)) {
                l = m + 1;
            } else {
                r = m;
            }
        }

        return l - 1;
    }

    boolean check(long t, int[] batteries, int n) {
        long sum = 0;
        for (int batterie : batteries) {
            sum += Math.min(batterie, t);
        }

        return sum / t >= n;
    }

}
